<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="baTable.form.operate ? true : false"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    :disabled="baTable.form.operate == 'view'"
                    class="flex"
                    >
                    <!-- :disabled="baTable.form.items!.TJFLAG == '1'" -->
                <FormItem :label="t('xPotsq.TKID')" type="string" v-model="baTable.form.items!.TKID" prop="TKID" :input-attr="{ placeholder: '保存时自动生成',disabled:true }" />
                <FormItem :label="t('xPotsq.CORP')" 
                v-model="baTable.form.items!.CORP" 
                prop="CORP" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'CORP',
                    field: 'CORP',
                    'remote-url': 'admin/corplib/index'
                }" 
                />
                <FormItem :label="t('xPotsq.YWTYPE')" 
                v-model="baTable.form.items!.YWTYPE" 
                prop="YWTYPE" 
                type="select"
                :data="{ content: { '进口': '进口', '出口': '出口','LBL':'LBL' } }" 
                />
                <FormItem :label="t('xPotsq.DEPT')" 
                v-model="baTable.form.items!.DEPT" 
                prop="DEPT" 
                type="remoteSelect"
                :input-attr="{
                    pk:'DEPTID',
                    field: 'DEPTNAME',
                    'remote-url':dEPARTMENT +'index',
                    placeholder: t('Click Select'),
                }"
                />
                <FormItem :label="t('xPotsq.YBH')" type="string" v-model="baTable.form.items!.YBH" prop="YBH" :input-attr="{ placeholder: t('Please input field', { field: t('xPotsq.YBH') }) }" />
                <!-- <FormItem :label="t('xPotsq.GOODSID')" type="string" v-model="baTable.form.items!.GOODSID" prop="GOODSID" :input-attr="{ placeholder: t('Please input field', { field: t('xPotsq.GOODSID') }) }" /> -->
                <!-- <FormItem :label="t('xPotsq.GOODS')" type="string" v-model="baTable.form.items!.GOODS" prop="GOODS" :input-attr="{ placeholder: t('Please input field', { field: t('xPotsq.GOODS') }) }" /> -->
                <FormItem :label="t('xPotsq.CONSIZE')" 
                v-model="baTable.form.items!.CONSIZE" 
                prop="CONSIZE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'code',
                    field: 'codename',
                    'remote-url': codelib  + 'now?where=consize'
                }"
                />
                <FormItem :label="t('xPotsq.CONTYPE')" 
                v-model="baTable.form.items!.CONTYPE" 
                prop="CONTYPE" 
                type="remoteSelect"
                :input-attr="{
                    pk: 'code',
                    field: 'codename',
                    'remote-url': codelib  + 'now?where=potkind'
                }"
                />
                <FormItem :label="t('xPotsq.CONSUM')" type="number" prop="CONSUM" v-model.number="baTable.form.items!.CONSUM" :input-attr="{ step: '1', placeholder: t('Please input field', { field: t('xPotsq.CONSUM') }) }" />
                <FormItem :label="t('xPotsq.REMARK')" style="width:100%" type="textarea" v-model="baTable.form.items!.REMARK" prop="REMARK" :input-attr="{ placeholder: t('Please input field', { field: t('xPotsq.REMARK') }) }" />
                <!-- <FormItem :label="t('xPotsq.TJFLAG')" type="string" v-model="baTable.form.items!.TJFLAG" prop="TJFLAG" :input-attr="{ placeholder: t('Please input field', { field: t('xPotsq.TJFLAG') }) }" /> -->
                <FormItem :label="t('xPotsq.OP')" type="string" v-model="baTable.form.items!.OP" prop="OP" :input-attr="{ placeholder: '当前操作人' ,disabled:true}" />
                <FormItem :label="t('xPotsq.OPTIME')" type="datetime" v-model="baTable.form.items!.OPTIME" prop="OPTIME" :input-attr="{ placeholder: '当前操作时间',disabled:true }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer v-if="baTable.form.operate !== 'view'">
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="handleSubmit" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
                <el-button v-if="baTable.form.items!.TJFLAG == '0'||baTable.form.items!.TJFLAG == null" type="primary" @click="handleTj">提交申请</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { ElMessage } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { codelib, dEPARTMENT } from '/@/api/controllerUrls'
import createAxios from '/@/utils/axios'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass
console.log(baTable);
const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    YWTYPE: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xPotsq.YWTYPE') })})],
    CONSIZE: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xPotsq.CONSIZE') })})],
    CONTYPE: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xPotsq.CONTYPE') })})],
    CONSUM: [buildValidatorData({name: 'required', message: t('Please enter the correct field', { field: t('xPotsq.CONSUM') })})],
    OPTIME: [buildValidatorData({name: 'date', message: t('Please enter the correct field', { field: t('xPotsq.OPTIME') })})],
})


const handleSubmit = ()=>{
    baTable.onSubmit(formRef.value,(res:any)=>{
        baTable.form.items!.TJFLAG = '0'
        baTable.form.items!.TKID = res.data
    })
}

const handleTj = ()=>{
    createAxios({
        url:'admin/XPotsq/tj',
        params:{
            TKID:baTable.form.items!.TKID
        }
    }).then((res:any)=>{
        baTable.getIndex()
        baTable.toggleForm('')
        ElMessage({
            message:'提交成功！',
            type:'success'
        })
    })
}

</script>

<style scoped lang="scss"></style>
